package com.dsjy.yf.address.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dsjy.yf.address.config.base.BizException;
import com.dsjy.yf.address.config.base.CommonEnum;
import com.dsjy.yf.address.entity.dataobject.CommunityDO;
import com.dsjy.yf.address.entity.myenum.OperateEnum;
import com.dsjy.yf.address.mapper.CommunityMapper;
import com.dsjy.yf.address.service.CommunityService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.text.DecimalFormat;
import java.util.List;

/**
 * @author whj
 * @version 1.0
 * @date 2021-01-19 10:17
 */
@Service
public class CommunityServiceImpl implements CommunityService {
    @Resource
    private CommunityMapper communityMapper;


    private static DecimalFormat decimalFormat = new DecimalFormat("000");

    @Override
    public Integer serchMaxSonCode(Integer parentCode) {
        Integer maxSonCode = communityMapper.serchMaxSonCode(parentCode);
        if (maxSonCode == null) {
            maxSonCode = 1;
        }
        return maxSonCode;
    }

    @Override
    @Cacheable(value = "community", key = "getMethodName()+#townCode+#flag")
    public List<CommunityDO> searchWithTownCode(Integer townCode, Boolean flag) {
        return communityMapper.searchWithTownCode(townCode, flag);
    }

    @Override
    @Cacheable(value = "community", key = "getMethodName()")
    public List<CommunityDO> searchAll() {
        return communityMapper.searchAll();
    }

    @Override
    @Cacheable(value = "community", key = "getMethodName()")
    public List<CommunityDO> searchCommunityOnly() {
        return communityMapper.searchNameOnly();
    }

    @Override
    @CacheEvict(value = "community", allEntries = true)
    @Transactional(rollbackFor = RuntimeException.class)
    public Integer addUpdDel(String operate, CommunityDO communityDO) {
        Integer result = null;
        if (OperateEnum.ADD.getOperate().equals(operate) && communityDO.getCommunityName() != null) {
            result = addCommunity(communityDO);
        } else if (OperateEnum.UPDATE.getOperate().equals(operate) && communityDO.getId() != null) {
            result = updCommunity(communityDO);
        }
        return result;
    }

    @Override
    public Integer addCommunity(CommunityDO communityDO) {
        if (communityDO.getParentCode() == null || String.valueOf(communityDO.getParentCode()).length() != 9) {
            throw new BizException(CommonEnum.PARAM_ERROR);
        }
        Integer sonCode = serchMaxSonCode(communityDO.getParentCode());
        if (sonCode != null) {
            sonCode++;
            communityDO.setCommunityCode(communityDO.getParentCode() + decimalFormat.format(sonCode));
            communityDO.setSonCode(sonCode);
            communityDO.setCreateTime(new Timestamp(System.currentTimeMillis()));
            return communityMapper.addCommunity(communityDO);
        }
        return null;
    }


    @Override
    public Integer updCommunity(CommunityDO communityDO) {
        communityDO.setUpdateTime(new Timestamp(System.currentTimeMillis()));
        return communityMapper.updCommunity(communityDO);
    }

    @Override
    public IPage searchWithPage(Integer pageNum, Integer pageSize, QueryWrapper wrapper) {
        wrapper.setEntityClass(CommunityDO.class);
        Page<CommunityDO> page = new Page<>(pageNum, pageSize);
        return communityMapper.selectPage(page, wrapper);

    }
}
